package com.lc.oss.utils;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.alibaba.fastjson2.JSONObject;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.comm.ResponseMessage;
import com.aliyun.oss.model.PutObjectResult;
import com.lc.oss.properties.AliYunOssProperties;
import com.lc.oss.properties.QiNiuOssProperties;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.study.common.constant.LocalConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @description: some desc
 * @author: LChuang
 * @date: 2023/11/30 17:14
 */
@Slf4j
@Component
public class OssUtil {

    @Resource
    private UploadManager qiNiuUploadManager;

    @Resource
    private QiNiuOssProperties qiNiuOssProperties;


    @Resource
    private OSSClient aliyunOssClient;

    @Resource
    private AliYunOssProperties aliYunOssProperties;


    /**
     * return 上传地址url
     */
    public String qiNiuUpLoad(MultipartFile file){
        try {
            String filename = file.getOriginalFilename();
            assert filename != null;
            String newFileName = qiNiuOssProperties.getFolder().concat(String.valueOf(System.currentTimeMillis())).concat(filename.substring(filename.lastIndexOf(".")));
            // 获取七牛云上传token
            Auth auth = Auth.create(qiNiuOssProperties.getAccessKey(), qiNiuOssProperties.getSecretKey());
            String token = auth.uploadToken(qiNiuOssProperties.getBucket());
            Response response = qiNiuUploadManager.put(file.getBytes(), newFileName, token);
            JSONObject jsonObject = JSONObject.parseObject(response.bodyString());
            log.info("七牛云文件上传结果：key:{},hash:{}", jsonObject.getString("key"), jsonObject.getString("hash"));
            // 存储空间需设置为公开空间才可通过返回的url访问
            return LocalConstant.HTTP.concat(qiNiuOssProperties.getDomain()).concat(LocalConstant.LEFT_SLASH).concat(jsonObject.getString("key"));
        } catch (IOException e) {
            ExceptionUtil.stacktraceToString(e);
        }
        return null;
    }


    /**
     * return 上传地址url
     */
    public String aliyunUpload(MultipartFile file){
        try {
            String filename = file.getOriginalFilename();
            assert filename != null;
            String newFileName = aliYunOssProperties.getFolder().concat(String.valueOf(System.currentTimeMillis())).concat(filename.substring(filename.lastIndexOf(".")));
            if (!aliyunOssClient.doesBucketExist(aliYunOssProperties.getBucket())) {
                log.info("阿里云OSS指定的存储空间【{}】不存在", aliYunOssProperties.getBucket());
                throw new RuntimeException("阿里云OSS指定bucket不存在");
            }
            PutObjectResult result = aliyunOssClient.putObject(aliYunOssProperties.getBucket(), newFileName, file.getInputStream());
            log.info("阿里云OSS上传requestId：{}", result.getRequestId());
            return LocalConstant.HTTPS.concat(aliYunOssProperties.getBucket()).concat(LocalConstant.POINT).concat(aliYunOssProperties.getEndpoint()).concat(LocalConstant.LEFT_SLASH).concat(newFileName);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}
